namespace my_vector
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin() { return _start; }
		iterator end() { return _finish; }
		const_iterator begin() const { return _start; }
		const_iterator end() const { return _finish; }

		size_t size() const { return _finish - _start; }
		size_t capacity() const { return _endofstorage - _start; }

		vector() :_start(nullptr), _finish(nullptr), _endofstorage(nullptr) {}
		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}

		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
			:_start(nullptr), _finish(nullptr), _endofstorage(nullptr)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		vector(const vector<T>& v)
			:_start(nullptr), _finish(nullptr), _endofstorage(nullptr)
		{
			vector<T> tmp(v.begin(), v.end());
			swap(tmp);
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				T* tmp = new T[n];
				size_t bsize = size();
				for (size_t i = 0; i < size(); i++)
					tmp[i] = _start[i];
				delete[] _start;
				_start = tmp;
				_finish = _start + bsize;
				_endofstorage = _start + n;

				//cout << "reserve(size_t n): " << capacity() << endl;
			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				for (size_t i = size(); i < n; i++)
					push_back(val);
			}
		}

		T& operator[](size_t pos)
		{
			assert(pos >= 0 && pos < size());
			return _start[pos];
		}
		const T& operator[](size_t pos) const 
		{
			assert(pos >= 0 && pos < size());
			return _start[pos];
		}

		void push_back(const T& val)
		{
			if (size() == capacity())
				reserve(_finish == nullptr ? 4 : capacity() * 2);

			*_finish++ = val;
		}

		void pop_back()
		{
			/*assert(!empty());
			_finish--;*/

			erase(_finish - 1);
		}

		iterator insert(iterator pos, const T& val)
		{
			assert(pos >= begin() && pos <= end());

			iterator ptr = end();
			while (ptr > pos)
			{
				*ptr = *(ptr - 1);
				ptr--;
			}
			*pos = val;
			_finish++;

			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= begin() && pos < end());

			iterator begin = pos;
			while (begin < end() - 1)
			{
				*begin = *(begin + 1);
				++begin;
			}
			--_finish;

			return pos;
		}

		void print()
		{
			//for (auto e : (*this))
			//	cout << e < ' ';

			for (size_t i = 0; i < size(); i++)
				cout << _start[i] << ' ';
			cout << endl;

			//iterator begin = _start;
			//iterator end = finish;
			//while (begin != end)
			//{
			//	cout << *begin << ' ';
			//	begin++;
			//}
		}

		bool empty() const { return size() == 0; }

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};
}
